package com.zd.kids.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zd.kids.DataBean.TrainTypeListDataBean;
import com.zd.kids.R;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.ui.activity.PicBookActivity;
import com.zd.kids.ui.activity.TestTrainActivity;
import com.zd.kids.ui.activity.TrainTypeListActivity;
import com.zd.kids.ui.activity.TrainWordActivity;
import com.zd.kids.ui.activity.VideoRecordActivity;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * @ClassName: com.zd.kids.ui.adapter
 * @Description:
 * @Author: hyy
 * @Date: 2020/6/14
 * @Time: 5:33 PM
 */
public class TrainTypeListAdapter extends BaseQuickAdapter<TrainTypeListDataBean, BaseViewHolder> {


    private int selectPositon = -1;

    public int getSelectPositon() {
        return selectPositon;
    }

    public void setSelectPositon(int selectPositon) {
        this.selectPositon = selectPositon;
    }

    public TrainTypeListAdapter(@Nullable List<TrainTypeListDataBean> data) {
        super(R.layout.activity_train_type_item, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, TrainTypeListDataBean item) {

        ImageView img_content = helper.getView(R.id.img_content);
        img_content.setImageResource(item.getImgRes());

        View view_top = helper.getView(R.id.view_top);
        View view_next_top = helper.getView(R.id.view_next_top);
        View view_arrow = helper.getView(R.id.view_arrow);
        View view_bottom = helper.getView(R.id.view_bottom);
        ImageView img_tag = helper.getView(R.id.img_tag);
        TextView tv_name = helper.getView(R.id.tv_name);
        tv_name.setText(item.getType_name());


        if (item.getComplete()) {
            if (selectPositon != -1 && selectPositon == helper.getAbsoluteAdapterPosition() - 1) {
                view_top.setVisibility(View.VISIBLE);
                view_arrow.setVisibility(View.VISIBLE);
            } else {
                view_top.setVisibility(View.INVISIBLE);
            }
            selectPositon = helper.getAbsoluteAdapterPosition();
            view_bottom.setVisibility(View.VISIBLE);
            view_next_top.setVisibility(View.VISIBLE);
            img_tag.setImageResource(R.mipmap.ic_train_type_select);

        } else {
            if (selectPositon != -1 && selectPositon == helper.getAbsoluteAdapterPosition() - 1) {
                view_top.setVisibility(View.VISIBLE);
                view_arrow.setVisibility(View.VISIBLE);
            } else {
                view_top.setVisibility(View.INVISIBLE);
            }

            view_bottom.setVisibility(View.INVISIBLE);
            view_next_top.setVisibility(View.INVISIBLE);
            img_tag.setImageResource(R.mipmap.ic_train_type_default);
        }

        if (helper.getAbsoluteAdapterPosition() == getItemCount() - 1) {
            view_bottom.setVisibility(View.INVISIBLE);
            view_next_top.setVisibility(View.INVISIBLE);
        }

        if (helper.getAbsoluteAdapterPosition() == 0) {
            view_top.setVisibility(View.INVISIBLE);
        }


        LinearLayout ll_rootview = helper.getView(R.id.ll_rootview);
        ll_rootview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (helper.getAbsoluteAdapterPosition() == 0) {
                    Intent intent = new Intent(mContext, VideoRecordActivity.class);
                    intent.putExtra(TrainTypeListActivity.UNIT_ID_KEY, item.getUnitid_id() + "");
                    mContext.startActivity(intent);
                } else if (helper.getAbsoluteAdapterPosition() == 1) {
                    Intent intent = new Intent(mContext, PicBookActivity.class);
                    intent.putExtra(TestTrainActivity.UNIT_ID_KEY, item.getUnitid_id() + "");
                    mContext.startActivity(intent);

                } else if (helper.getAbsoluteAdapterPosition() == 3) {
                    Intent intent = new Intent(mContext, TestTrainActivity.class);
                    intent.putExtra(TestTrainActivity.UNIT_ID_KEY, item.getUnitid_id() + "");
                    mContext.startActivity(intent);
                    if (mContext instanceof Activity) {
                        ((Activity) mContext).finish();
                    }
                } else {
                    Intent intent = new Intent(mContext, TrainWordActivity.class);
                    intent.putExtra(TrainWordActivity.WORD_ID_KEY, item.getUnitid_id() + "");
                    mContext.startActivity(intent);
                }

            }
        });

    }
}
